package com.dongxing.concyrrency.chapter9;

import java.util.stream.Stream;

/**
 * @author XavierDong
 **/
public class DifferenceOfWaitAndSleep {

    private final static Object LOCL = new Object();

    public static void main(String[] args) {

        Stream.of("T1", "T2").forEach(name -> new Thread(name) {
            @Override
            public void run() {
//                DifferenceOfWaitAndSleep.m1();
                m2();
            }
        }.start());

//        m1();
//        m2();
    }

    private static void m1() {

        synchronized (LOCL) {
            try {
                System.out.println("The Thread " + Thread.currentThread().getName() + " enter.");
                Thread.sleep(20_000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    private static void m2() {
        synchronized (LOCL) {
            try {
                System.out.println("The Thread " + Thread.currentThread().getName() + " enter.");
                LOCL.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
